Skip to content

Return fetched rows in row ID order unless the filter orders them - #7

Merged
tetujin merged 1 commit into
mainfrom
fix/fetch-order-by-rowid
Nov 18, 2026
Merged

tetujin merged 1 commit into
mainfrom
fix/fetch-order-by-rowid

Conversation

@tetujin

@tetujin tetujin commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • SQLiteEngine.fetch(filter:limit:) ran SELECT * FROM t WHERE <filter> LIMIT n with no ORDER BY. SQL does not define the row order then.
  • The sync reads id > lastUploadedId with LIMIT batchSize, then moves its marker, and deletes rows, up to the last row read. It relies on the batch being the lowest IDs, in order. SQLite returns them that way today (rowid range search), but not by contract. With another order (reproduced with PRAGMA reverse_unordered_selects), a batch of [10, 9, 8, 7] would upload 7–10, delete 1–7 (1–6 never uploaded), and send 8–10 again. AWDataTransferManager (applewatch) pages the same way.
  • fetch now appends ORDER BY rowid (id is the rowid) unless the filter has its own ORDER BY, e.g. com.awareframework.ios.context's "timestamp >= … ORDER BY timestamp DESC", which keeps working.

Impact

  • Query plan unchanged: SEARCH t USING INTEGER PRIMARY KEY (rowid>?), no sort step. Measured 0.116 ms vs 0.117 ms for 1,000 rows of a 1M-row table.
  • Callers get the same rows as today, since SQLite already returned them in ID order. ORDER BY rowid (rather than id) also works for tables without an id column.
  • Checked all fetch(filter:limit:) callers in aware-client-ios-v3: the sync, applewatch transfer (benefit), context (own ORDER BY, unchanged), aware-client fallback and tests (unchanged).

Test plan

  • swift test: 91 tests pass
  • testFetchReturnsRowsInIdOrder fails without the change ([30, 29, 28] instead of [23, 24, 25] under reverse_unordered_selects) and passes with it
  • A filter with its own ORDER BY (upper and lower case) keeps its order; containsOrderBy does not match words like border

🤖 Generated with Claude Code

SQL does not define the order of rows without ORDER BY. The sync reads
`id > lastUploadedId` with `LIMIT batchSize` and moves its marker, and
deletes rows, up to the last row it read, so it relies on the batch being
the lowest IDs in order. SQLite returns them in that order today, but not by
contract: with the rows in another order (shown with PRAGMA
reverse_unordered_selects), rows that were not uploaded would be skipped and
deleted, and others sent twice. The Apple Watch transfer pages the same way.

- fetch(filter:limit:) appends ORDER BY rowid (id is the rowid) unless the
  filter has its own ORDER BY, as in "... ORDER BY timestamp DESC". The
  query plan is unchanged (rowid range search), and tables without an `id`
  column still work.
- Tests: ID order under reverse_unordered_selects (fails without this
  change), a filter with its own ORDER BY, and the ORDER BY detection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tetujin
tetujin merged commit e461ebf into main Nov 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant